// 地址相关接口
import type { AddressItem, AddressParams } from '@/types/address'
import request from '@/utils/request'

enum API {
  // 添加收货地址
  addAddress_url = '/member/address',
  // 获取收货地址列表
  getAddressList_url = '/member/address',
  // 修改收货地址
  updateAddress_url = '/member/address/',
  // 获取收货地址详情
  getAddressById_url = '/member/address/',
  // 删除收货地址
  delAddress_url = '/member/address/'
}

type AddressData = {
  id: number
}

/**
 * 添加收货地址
 * @param {AddressParams} data 携带参数
 * @returns
 */
export const reqAddAddress = (data: AddressParams) => {
  return request<AddressData>({
    url: API.addAddress_url,
    method: 'POST',
    data
  })
}

/**
 * 获取收货地址列表
 * @returns
 */
export const reqGetAddressList = () => {
  return request<AddressItem[]>({
    url: API.getAddressList_url,
    method: 'GET'
  })
}

/**
 * 通过 id 获取收货地址详情
 * @param {string} id 收货地址 id (路径参数)
 * @returns
 */
export const reqGetAddressById = (id: string) => {
  return request<AddressItem>({
    url: API.getAddressById_url + id,
    method: 'GET'
  })
}

/**
 * 修改收货地址
 * @param {string} id 收货地址 id (路径参数)
 * @param {AddressParams} data 请求体参数 (地址的相关信息)
 * @returns
 */
export const reqUpdateAddress = (id: string, data: AddressParams) => {
  return request({
    url: API.updateAddress_url + id,
    method: 'PUT',
    data
  })
}

/**
 * 删除收货地址
 * @param {string} id 删除地址的 id
 * @returns
 */
export const reqDelAddress = (id: string) => {
  return request<AddressData>({
    url: API.delAddress_url + id,
    method: 'DELETE'
  })
}
